﻿using System.Collections.Generic;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using Twins.Infrastructure;
using Twins.Interface;
using Twins.Interface.IHandler;

namespace Twins.Classes.Hanlder.HanlderStatement
{
    public class HanlderForIIfStatement : IHandler<IIfStatement>
    {
        public IEnumerable<ILineCode> DoFor(IIfStatement ifStatement)
        {
            var hashLineCode = ifStatement.IfKeyword.GetText();
            var receiveHashLineCodeFromProcesserCSharpExpression = IoC
                .Resolve<IReceiveHashLineCodeFromProcesserCSharpExpression>();
            receiveHashLineCodeFromProcesserCSharpExpression
                .ReceiveHashLineCode(ifStatement.Condition, ref hashLineCode);
            var receiveHashLineCodeFromProcesserCSharpStatement = IoC
               .Resolve<IReceiveHashLineCodeFromProcesserCSharpStatement>();
            var hashLineCodesThen = receiveHashLineCodeFromProcesserCSharpStatement
                .ReceiveHashLineCode(ifStatement, ifStatement.Then, hashLineCode);                          
            if (ifStatement.ElseKeyword != null)
            {
                var hashLineCodesElse = receiveHashLineCodeFromProcesserCSharpStatement
                .ReceiveHashLineCode(ifStatement,ifStatement.Else, ifStatement.ElseKeyword.GetText());
                var hashLineCodes = new List<ILineCode>
                                        {
                                            new LineCode(ifStatement,
                                                         hashLineCodesThen[0].HashLineCode +
                                                         hashLineCodesElse[0].HashLineCode)
                                        };
                hashLineCodesThen.Remove(hashLineCodesThen[0]);
                hashLineCodes.AddRange(hashLineCodesThen);
                hashLineCodesElse.Remove(hashLineCodesElse[0]);
                hashLineCodes.AddRange(hashLineCodesElse);
                return hashLineCodes;
            }
            return hashLineCodesThen;
        }

        public IEnumerable<ILineCode> DoFor(object obj)
        {
            return DoFor((IIfStatement)obj);
        }
    }


}
